/**
 * Created by codeczx on 2018/3/20.
 */
public class OutputRunnable implements Runnable {

    private int num;
    private Object lock;

    OutputRunnable(Object lock, int num) {
        this.num = num;
        this.lock = lock;
    }

    @Override
    public void run() {
        try {
            while (true) {
                synchronized (lock) {
                    System.out.println(num);
//                    System.out.println(num+" notifyAll");
                    lock.notifyAll();
//                    System.out.println(num+" start wait");
                    lock.wait();
//                    System.out.println(num+" end wait");
                    Thread.sleep(5000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Object lock = new Object();
        new Thread(new OutputRunnable(lock,1)).start();
        new Thread(new OutputRunnable(lock,2)).start();
    }
}
